import { Module } from "vuex";
import { IRootState } from "../../types";
import { ISystemState } from "./type";
import {
  getUserListRequest,
  deleteUserListRequest,
  createUesrListRequest,
  editUserListRequest
} from "@/service/main/system/system";
import { ElMessage } from "element-plus";

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      departmentList:[],
      departmentCount:0
    };
  },
  mutations: {
    //存储用户数据
    changeUsersList(state, userList: any) {
      state.usersList = userList;
    },
    changeUsersCount(state, totalCount: number) {
      state.usersCount = totalCount;
    },
    //存储role数据

    changeRoleList(state, roleList: any) {
      state.roleList = roleList;
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount;
    },

    // 储存商品信息

    changeGoodsList(state, goodsList: any) {
      state.goodsList = goodsList;
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount;
    },

    // 存储用户菜单列表
    changeMenuList(state, menuList: any) {
      state.menuList = menuList;
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount;
    },

    //储存部门列表
    changeDepartmentList(state, departmentList: any) {
      state.departmentList = departmentList;
    },
    changeDepartmentCount(state, departmentCount: number) {
      state.departmentCount = departmentCount;
    },
  },
  getters: {
    // 获取用户列表信息
    pageListData(state) {
      // return (pageName:string) =>{
      //     // pageName = pageName.slice(0,1).toLowerCase + pageName.slice(1)
      //     return state[`${pageName}List`]
      // }
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
        // switch (pageName) {
        //     case 'Users':
        //         return state.userList
        //         break;
        //     case 'Role':
        //         return state.roleList
        //         break;
        // }
      };
    },
    // 获取用户列表数量
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    },
  },
  actions: {
    //发起请求，得到userList 数据
    async getPageListAcion({ commit }, payload: any) {
      //根据当前页面的名称来转换请求的地址
      const pageName = payload.pageName;
      // console.log(pageName);

      const pageUrl = `${pageName}/list`;
      //对页面发起请求
      const pageResult = await getUserListRequest(pageUrl, payload.queryInfo);
      // console.log(userList);
      const { list, totalCount } = pageResult.data;

      //将获得的数据进行存储
      // commit('changeUserList',list)
      // commit('changeUserCount',totalCount)

      //使用模板字符串进行拼接
      const changeName = pageName.slice(0, 1).toUpperCase() + pageName.slice(1);
      commit(`change${changeName}List`, list);
      commit(`change${changeName}Count`, totalCount);
    },

    //发起请求删除数据
    async deletePageListAction({ dispatch }, payload: any) {
      const pageName = payload.pageName;
      const id = payload.id;
      const url = `/${pageName}/${id}`;

      //发起网络请求
      await deleteUserListRequest(url);

      //重新请求数据，通过dispatch 调用函数
      dispatch("getPageListAcion", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },

    //发起创建用户的请求

    async createPageListAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createUesrListRequest(pageUrl,newData)
      ElMessage.success('创建成功')
      //调用接口，重新请求数据
      dispatch("getPageListAcion", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },

    //发起编辑用户的请求

    async editPageListAction({dispatch},payload:any){
        const { pageName,id,editData} = payload
        const pageUrl = `/${pageName}/${id}`
        await editUserListRequest(pageUrl,editData)
        ElMessage.success('编辑成功')
        dispatch("getPageListAcion", {
            pageName,
            queryInfo: {
              offset: 0,
              size: 10,
            },
          });
          
    }
  },
};

export default systemModule;
